public class Rationalcalculate {
    Rational add(Rational r,Rational l) {  //加法运算
        int a=r.getNumerator();
        int b=r.getDenominator();
        int newNumerator=l.numerator*b+l.denominator*a; //计算出新分子
        int newDenominator=l.denominator*b;           //计算出新分母
        Rational result=new Rational();
        result.setNumerator(newNumerator);
        result.setDenominator(newDenominator);
        return result;
    }
    Rational sub(Rational l,Rational r) {  //减法运算
        int a=r.getNumerator();
        int b=r.getDenominator();
        int newNumerator=l.numerator*b-l.denominator*a;
        int newDenominator=l.denominator*b;
        Rational result=new Rational();
        result.setNumerator(newNumerator);
        result.setDenominator(newDenominator);
        return result;
    }
    Rational muti(Rational l,Rational r) { //乘法运算
        int a=r.getNumerator();
        int b=r.getDenominator();
        int newNumerator=l.numerator*a;
        int newDenominator=l.denominator*b;
        Rational result=new Rational();
        result.setNumerator(newNumerator);
        result.setDenominator(newDenominator);
        return result;
    }
    Rational div(Rational l,Rational r)  { //除法运算
        int a=r.getNumerator();
        int b=r.getDenominator();
        int newNumerator=l.numerator*b;
        int newDenominator=l.denominator*a;
        Rational result=new Rational();
        result.setNumerator(newNumerator);
        result.setDenominator(newDenominator);
        return result;
    }
}
